/**
 * 
 */
package remato.client.view.util;

import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;

import remato.client.view.editor.properties.PropertyLinkDescriptor;

/**
 * @author petterei
 *
 * @version $Id: LinkListPropertySource.java,v 1.1 2006-01-23 19:58:37 petterei Exp $
 */
public class LinkListPropertySource implements IPropertySource {

	private String label = "";
	private List<IAdaptable> items = null;
	private Map<IAdaptable, PropertyDescriptor> descriptors = new Hashtable<IAdaptable, PropertyDescriptor>();
	
	public LinkListPropertySource(List<IAdaptable> items, String... label) {
		if (0 < label.length) {
			this.label = label[0];
		}
		this.items = items;
	}
	public LinkListPropertySource(IAdaptable item, String... label) {
		this(Arrays.asList(new IAdaptable[]{item}), label);
	}
	
	public Object getEditableValue() {
		return this;
	}

	public IPropertyDescriptor[] getPropertyDescriptors() {
		for (IAdaptable adaptable : items){
			PropertyDescriptor descriptor = (PropertyDescriptor)adaptable.getAdapter(PropertyLinkDescriptor.class);
			if (null != descriptor) {
				descriptors.put(adaptable, descriptor);
			}
		}
		IPropertyDescriptor[] descriptorArray = new IPropertyDescriptor[descriptors.size()]; 
		int i = 0;
		for (IPropertyDescriptor descriptor : descriptors.values()) {
			descriptorArray[i] = descriptor;
			i++;
		}
		return descriptorArray;
	}

	public Object getPropertyValue(Object id) {
		return descriptors.get(id);
	}

	public boolean isPropertySet(Object id) {
		return descriptors.containsKey(id);
	}

	public void resetPropertyValue(Object id) {}

	public void setPropertyValue(Object id, Object value) {}

	@Override
	public String toString() {
		return label;
	}

	public List<IAdaptable> getItems() {
		return items;
	}
}

